using System;
using System.Collections.Generic;
using System.Text;

using Outlook = Microsoft.Office.Interop.Outlook;

namespace Microsoft.SoftwarePlusServices.ReferenceBits.OutlookPlusServices
{
    ///
    /// <summary>
    /// This class is a base class for all outlook folders that are used.  Creation of a folder 
    /// requires several steps, specifically adding items and views to folders.  This class abstracts 
    /// that into the creation process.
    /// </summary>
    /// 
    public abstract class OutlookFolder
    {
        #region Private Fields
        private Outlook.Folder _folder;
        #endregion

        #region Constructor

        ///
        /// <summary>
        /// Initializes a new instance of the object and verifies that the actual Outlook folders exists.  
        /// If it doesn't, the folder is created and initialized using the derived classes methods.
        /// </summary>
        /// <param name="parent">The Outlook folder that contains this folder.</param>
        /// <param name="name">The name of the new folder.</param>
        ///
        protected OutlookFolder(Outlook.Folder parent, string name)
        {
            // find the folder
            _folder = FindFolder(parent, name);

            // if the folder didn't exist, create a new one
            if (_folder == null)
            {
                // create the new folder
                _folder =
                    (Outlook.Folder)parent.Folders.Add(
                        name, Outlook.OlDefaultFolders.olFolderInbox);

                // setup the folder properties and views
                AddFolderItemProperties(_folder.UserDefinedProperties);
                AddFolderViews(_folder.Views);
            }
        }
        #endregion

        #region Protected Properties

        ///
        /// <summary>
        /// A reference to the embedded Outlook folder object.
        /// </summary>
        ///
        public Outlook.Folder Folder
        {
            get { return _folder; }
        }
        #endregion

        #region Protected Methods

        ///
        /// <summary>This method is the base method that will be implemented by derived classes.  
        /// Any properties that are specific to the derived folder type will be added directly to 
        /// the Outlook folder's UserDefinedProperties collection.
        /// </summary>
        /// <param name="properties">The UserDefinedProperties object of the contained Outlook folder.</param>
        ///
        protected virtual void AddFolderItemProperties(Outlook.UserDefinedProperties properties)
        { }

        ///
        /// <summary>This method is the base method that will be implemented by derived classes.  
        /// Any views that are specific to the derived folder type will be added directly to 
        /// the Outlook folder's Views collection.
        /// </summary>
        /// <param name="views">The Views object of the contained Outlook folder.</param>
        ///
        protected virtual void AddFolderViews(Outlook.Views views)
        { }

        ///
        /// <summary>
        /// This method is a helper method used to locate a named folder within a parent Outlook folder.
        /// </summary>
        /// <param name="parent">The parent folder to search for the child folder in.</param>
        /// <param name="name">The name of the folder to find.</param>
        /// <returns>Either a reference to the Outlook Folder object if the folder was found or null if it wasn't found.</returns>
        ///
        protected static Outlook.Folder FindFolder(Outlook.Folder parent, string name)
        {
            //look through each folder to find the one with the appropriate name
            foreach (Outlook.Folder folder in parent.Folders)
                if (folder.Name == name)
                    return folder;

            // if nothing was found, return null
            return null;
        }
        #endregion
    }
}
